package cn.work.control;
import cn.work.bean.servicebean;
import cn.work.service.goodsService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ServiceDeleteServlet", value = "/ServiceDeleteServlet")
public class ServiceDeleteServlet extends HttpServlet {
    private goodsService gdao = new goodsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");


        String query = request.getParameter("query");
        List<servicebean> serviceList = null;

        try {
            // 调用业务层，进行搜索
            serviceList = gdao.searchServices(query);
        } catch (SQLException e) {
            e.printStackTrace();
            response.getWriter().write("{\"error\":\"查询失败\"}");
            return;
        }

        // 将查询结果转换为 JSON 格式
        String jsonResponse = new com.google.gson.Gson().toJson(serviceList);

        // 输出 JSON 响应
        response.getWriter().write(jsonResponse);
            }
        }